package config;

public class MiddleManConfiguration {
	private int P; // Number of queues
	private int L; // Length of each queue
	private int M; // Total working threads
	private String[] databases;
	
	public MiddleManConfiguration(int _P, int _L, int _M, String[] _databases) {
		P = _P;
		L = _L;
		M = _M;
		databases = _databases.clone();
	}
	
	public MiddleManConfiguration(String a_str) throws IllegalArgumentException {
		if(a_str == null || a_str.startsWith("{")==false || a_str.endsWith("}")==false) {
			throw new IllegalArgumentException("[Doesn't start with { or end with } or is null]:\nString '"+a_str+"' is not a valid MiddleManConfiguration string");
		}
		// Eliminate { and }
		String str = a_str.substring(1, a_str.length()-1);
		
		String []pieces = str.split(",");
		int databases_count = pieces.length - 3; // 3 = P,L,M
		databases = new String[databases_count];
		int current_database = 0;
		boolean P_found = false, L_found = false, M_found = false;
		
		for(String piece:pieces) {
			if(piece.isEmpty()) {
				continue;
			}
			if(piece.startsWith("{")==false || piece.endsWith("}")==false) {
				throw new IllegalArgumentException("[Piece '"+piece+"' doesn't start with { or end with }]:\nString '"+str+"' is not a valid MiddleManConfiguration string");
			}
			// Eliminate { and }
			piece = piece.substring(1, piece.length()-1);
			String []info = piece.split(":");
			if(info.length != 2) {
				throw new IllegalArgumentException("[Piece '"+piece+"' is not composed of 2 subpieces]:\nString '"+str+"' is not a valid MiddleManConfiguration string");
			}
			String param_name = info[0];
			String param_value = info[1];
			
			if(param_name.equals("P")) {
				if(P_found) {
					throw new IllegalArgumentException("[P is present twice in the config string]:\nString '"+str+"' is not a valid MiddleManConfiguration string");
				} else {
					try {
						P = Integer.parseInt(param_value);
						P_found = true;
					} catch(NumberFormatException e) {
						throw new IllegalArgumentException("[P is not an int]:\nString '"+str+"' is not a valid MiddleManConfiguration string");
					}
				}
			} else if(param_name.equals("L")) {
				if(L_found) {
					throw new IllegalArgumentException("[L is present twice in the config string]:\nString '"+str+"' is not a valid MiddleManConfiguration string");
				} else {
					try {
						L = Integer.parseInt(param_value);
						L_found = true;
					} catch(NumberFormatException e) {
						throw new IllegalArgumentException("[L is not an int]:\nString '"+str+"' is not a valid MiddleManConfiguration string");
					}
				}
			} else if(param_name.equals("M")) {
				if(M_found) {
					throw new IllegalArgumentException("[M is present twice in the config string]:\nString '"+str+"' is not a valid MiddleManConfiguration string");
				} else {
					try {
						M = Integer.parseInt(param_value);
						M_found = true;
					} catch(NumberFormatException e) {
						throw new IllegalArgumentException("[M is not an int]:\nString '"+str+"' is not a valid MiddleManConfiguration string");
					}
				}
			} else if(param_name.equals("database")) {
				if(current_database >= databases_count) {
					throw new IllegalArgumentException("[we have extra parameters?]:\nString '"+str+"' is not a valid MiddleManConfiguration string");
				} else {
					databases[current_database] = param_value;
					current_database ++;
				}
			} else {
				throw new IllegalArgumentException("[Unrecognized "+param_name+"]:\nString '"+str+"' is not a valid MiddleManConfiguration string");
			}
		}
		
		if(P_found == false) {
			throw new IllegalArgumentException("[P was not found]:\nString '"+str+"' is not a valid MiddleManConfiguration string");
		}
		
		if(L_found == false) {
			throw new IllegalArgumentException("[L was not found]:\nString '"+str+"' is not a valid MiddleManConfiguration string");
		}
		
		if(M_found == false) {
			throw new IllegalArgumentException("[M was not found]:\nString '"+str+"' is not a valid MiddleManConfiguration string");
		}
		
		if(databases_count<1) {
			throw new IllegalArgumentException("[No databases??]:\nString '"+str+"' is not a valid MiddleManConfiguration string");
		}
		
	}
	
	public int P() {
		return P;
	}
	
	public int L() {
		return L;
	}
	
	public int M() {
		return M;
	}
	
	public String[] databases() {
		return databases.clone();
	}
	
	public String toString() {
		String ret = "";
		ret += "{";
			ret += "{P:"+P+"}";
			ret += ",{L:"+L+"}";
			ret += ",{M:"+M+"}";
			for(int i=0; i<databases.length; i++) {
				ret += ",{database:"+databases[i]+"}";
			}
		ret += "}";
		return ret;
	}
}
